import config from '../../utils/config';
import request from '../../utils/request';

let startY = 0;//手指起始的坐标
let moveY = 0;//手指移动的坐标
let moveDistance = 0;//手指移动的距离

Page({
    data: {
        coverTransform: 'translateY(0)',
        coverTransition: '',
        userInfo:{},
        recentPlayList:[]
    },
    onLoad: function(){
        // 从本地存储中获取用户信息
        const userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            this.setData({
                userInfo: JSON.parse(userInfo)  
            });
        }

        // 获取用户播放记录
        this.getRecentPlayList();
    },
    async getRecentPlayList(){
        const res = await request(config.recentPlayRecords, { 
            uid: this.data.userInfo.userId, 
            type: 1 
        });
        this.setData({
            recentPlayList: res.weekData
        });
    },
    handleTouchStart(e){
        // 去除过渡
        this.setData({
            coverTransition: ''
        });

        // 获取手指的起始坐标
        startY = e.touches[0].clientY;
        console.log('handleTouchStart',startY);
    },
    handleTouchMove(e){
        moveY = e.touches[0].clientY;
        moveDistance = moveY - startY;
        // 动态更新coverTransform的值

        // 不允许向上移动
        if(moveDistance <= 0){
            return;
        }

        // 控制移动的距离
        if(moveDistance >= 80){
            moveDistance = 80;
        }
        this.setData({
            coverTransform: `translateY(${ moveDistance }rpx)`
        });
        console.log('handleTouchMove',moveY);
    },
    handleTouchEnd(){
        // 归零
        this.setData({
            coverTransform: `translateY(0rpx)`,
            //加动效平滑过渡
            coverTransition: 'transform 1s linear'
        });
        console.log('handleTouchEnd');
    },
    toLogin(){
        wx.navigateTo({
            url: '/pages/login/login'
        });
    }
})